package com.example.practice;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;


/**
 * A simple {@link Fragment} subclass.
 */
public class RoadFragment extends Fragment {

    private View rootView;
    private TextView kuaihuan1;
    private TextView kuaihuan2;
    private TextView kuaihuan4;
    private TextView gaohuan3;
    private TextView lianxiang;
    private TextView yiyuan;
    private TextView xueyuan;
    private TextView xingfu;
    private TextView tcc;
    private TextView nyr;
    private TextView xq;
    private TextView lkwendu;
    private TextView lkshudi;
    private TextView lkpm25;
    private ImageView lkshuax;
    private TimerTask timerTask;
    private Timer timer;
    private int i=0;
    public RoadFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_road, container, false);
        initView();
        timer=new Timer();
        timerTask=new TimerTask(){
            @Override
            public void run() {
                initData();
            }
        };
        timer.schedule(timerTask,0,3000);
        return rootView;
    }
    private interface BlogSever{
        @GET("aircondition")
        Call<ResponseBody> getBlog();
    }
    private void initData() {
        Retrofit retrofit =new  Retrofit.Builder()
                .baseUrl("http://hh1.me:5001/")
                .build();
        BlogSever blogSever = retrofit.create(BlogSever.class);
        Call<ResponseBody> blog = blogSever.getBlog();
        blog.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String s = response.body().string();
                    ArrayList<Car7Users> list = new ArrayList<>();
                    Car7Users car7Users = new Gson().fromJson(s, Car7Users.class);
                    list.add(car7Users);
                    initUI(list);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private void initUI(final List<Car7Users> list) {
        textViewColor(kuaihuan1,list.get(0).get拥挤程度().get(0));
        textViewColor(kuaihuan2,list.get(0).get拥挤程度().get(0));
        textViewColor(kuaihuan4,list.get(0).get拥挤程度().get(0));
        textViewColor(gaohuan3,list.get(0).get拥挤程度().get(1));
        textViewColor(lianxiang,list.get(0).get拥挤程度().get(2));
        textViewColor(yiyuan,list.get(0).get拥挤程度().get(3));
        textViewColor(xueyuan,list.get(0).get拥挤程度().get(4));
        textViewColor(xingfu,list.get(0).get拥挤程度().get(5));
        textViewColor(tcc,list.get(0).get拥挤程度().get(6));
        if(i==0){
            i=1;
            wather(list);
        }
        lkshuax.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    i=0;
                    initData();
            }
        });

    }
    private void wather(List<Car7Users> list){
        SimpleDateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2=new SimpleDateFormat("EEEE");
        Date date = new Date(System.currentTimeMillis());
        nyr.setText(format1.format(date));
        xq.setText(format2.format(date));
        lkwendu.setText(list.get(0).getWendu()+"℃");
        lkshudi.setText(list.get(0).getShidu()+"%");
        lkpm25.setText(list.get(0).get_$Pm2539()+"ug/m3");
    }
    private void textViewColor(TextView textView ,int i){
        switch(i){
            case 1:
                textView.setBackgroundColor(Color.parseColor("#6ab82e"));
                break;
            case 2:
                textView.setBackgroundColor(Color.parseColor("#ece93a"));
                break;
            case 3:
                textView.setBackgroundColor(Color.parseColor("#f49b25"));
                break;
            case 4:
                textView.setBackgroundColor(Color.parseColor("#f49b25"));
                break;
            case 5:
                textView.setBackgroundColor(Color.parseColor("#e33532"));
                break;
            case 6:
                textView.setBackgroundColor(Color.parseColor("#b01e23"));
                break;

        }
    }
    private void initView() {
        i=0;
        kuaihuan1 = (TextView) rootView.findViewById(R.id.kuaihuan1);
        kuaihuan2 = (TextView) rootView.findViewById(R.id.kuaihuan2);
        kuaihuan4 = (TextView) rootView.findViewById(R.id.kuaihuan4);
        gaohuan3 = (TextView) rootView.findViewById(R.id.gaohuan3);
        lianxiang = (TextView) rootView.findViewById(R.id.lianxiang);
        yiyuan = (TextView) rootView.findViewById(R.id.yiyuan);
        xueyuan = (TextView) rootView.findViewById(R.id.xueyuan);
        xingfu = (TextView) rootView.findViewById(R.id.xingfu);
        tcc = (TextView)rootView.findViewById(R.id.tcc);
        nyr = (TextView) rootView.findViewById(R.id.nyr);
        xq = (TextView) rootView.findViewById(R.id.xq);
        lkwendu = (TextView) rootView.findViewById(R.id.lkwendu);
        lkshudi = (TextView) rootView.findViewById(R.id.lkshudi);
        lkpm25 = (TextView) rootView.findViewById(R.id.lkpm25);
        lkshuax = (ImageView) rootView.findViewById(R.id.lkshuax);

    }


}
